package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.MarketOrderGoods;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.bo.wx.WxOrderCommentBo;
import com.cskaoyan.bean.bo.wx.WxOrderSubmitBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.order.OrderDetailVo;
import com.cskaoyan.bean.vo.wx.order.OrderListVo;
import com.cskaoyan.bean.vo.wx.order.WxOrderSubmitVo;
import com.cskaoyan.service.wx.WxOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.hibernate.validator.internal.xml.config.ValidationXmlParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单
 * url:wx/order/list?showType=0&page=1&limit=10
 *
 * @Author: zhang
 * @Date: 2022/11/23 8:54
 * @Since: jdk1.8
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    WxOrderService wxOrderService;

    /**
     * 查询不同状态的订单
     *
     * @param showType 订单状态
     * @param page     页码
     * @param limit    限制
     * @return 订单信息
     */
    @RequestMapping("list")
    public BaseRespVo<OrderListVo> list(Integer showType, Integer page, Integer limit) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = user.getId();
        CommonData<OrderListVo> data = wxOrderService.list(userId, showType, page, limit);
        return BaseRespVo.ok(data);
    }


    /**
     * 订单详情信息
     *
     * @param orderId 订单id
     * @return 订单信息
     */
    @RequestMapping("detail")
    public BaseRespVo<OrderDetailVo> detail(Integer orderId) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = user.getId();
        OrderDetailVo data = wxOrderService.detail(userId, orderId);
        return BaseRespVo.ok(data);
    }

    /**
     * 申请退款
     *
     * @param map 订单id
     * @return ok
     */
    @PostMapping("refund")
    public BaseRespVo<Object> refund(@RequestBody Map<String, Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = user.getId();
        Integer orderId = map.get("orderId");
        wxOrderService.refund(userId, orderId);
        return BaseRespVo.ok();
    }

    /**
     * 取消订单
     *
     * @param map 订单id
     * @return ok
     */
    @PostMapping("cancel")
    public BaseRespVo<Object> cancel(@RequestBody Map<String, Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = user.getId();
        Integer orderId = map.get("orderId");
        wxOrderService.cancel(userId, orderId);
        return BaseRespVo.ok();
    }

    /**
     * 确认收货
     *
     * @param map 订单id
     * @return ok
     */
    @PostMapping("confirm")
    public BaseRespVo<Object> confirm(@RequestBody Map<String, Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = user.getId();
        Integer orderId = map.get("orderId");
        wxOrderService.confirm(userId, orderId);
        return BaseRespVo.ok();
    }

    /**
     * 删除订单
     *
     * @param map 订单id
     * @return ok
     */
    @PostMapping("delete")
    public BaseRespVo<Object> delete(@RequestBody Map<String, Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = user.getId();
        Integer orderId = map.get("orderId");
        wxOrderService.delete(userId, orderId);
        return BaseRespVo.ok();
    }

    /**
     * 删除订单
     *
     * @param map 订单id
     * @return ok
     */
    @PostMapping("prepay")
    public BaseRespVo<Object> prepay(@RequestBody Map<String, Integer> map) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = user.getId();
        Integer orderId = map.get("orderId");
        wxOrderService.prepay(userId, orderId);
        return BaseRespVo.ok();
    }

    /**
     * 订单评价
     *
     * @param orderId 订单id
     * @param goodsId 商品id
     * @return 订单信息
     */
    @GetMapping("goods")
    public BaseRespVo<MarketOrderGoods> goods(Integer orderId, Integer goodsId) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        MarketOrderGoods data = wxOrderService.goods(orderId, goodsId);
        return BaseRespVo.ok(data);
    }

    /**
     * 订单评价
     *
     * @param wxOrderCommentBo 评价信息
     * @return ok
     */
    @PostMapping("comment")
    public BaseRespVo<Object> comment(@RequestBody WxOrderCommentBo wxOrderCommentBo) {
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        wxOrderService.comment(wxOrderCommentBo);
        return BaseRespVo.ok();
    }

    /**
     * 购物车中下单
     * @param wxOrderSubmitBo 订单信息
     * @return ok
     */
    @PostMapping("submit")
    public BaseRespVo<WxOrderSubmitVo> submit(@RequestBody WxOrderSubmitBo wxOrderSubmitBo){
        Subject subject = SecurityUtils.getSubject();
        MarketUser user = (MarketUser) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.error(501, "请登录");
        }
        Integer userId = user.getId();
        WxOrderSubmitVo data=wxOrderService.submit(userId,wxOrderSubmitBo);
        return BaseRespVo.ok(data);
    }
}
